< previous page page_153 next page >

Page 153
the Printer object for positioning. Only when it must generate printed output does it use the actual object (picture box or printer) on which it is printing.
The following code shows a modified version of the PrintWrappedText subroutine used by the WordWrap program described earlier. It takes as input the object on which it should print, the number of the page it should print, the text, and the margins it should use. It breaks the text into words much as program WordWrap does.
As it examines the output words, the routine adjusts the Printer object's CurrentX and CurrentY properties. Only when it reaches the specified page does the routine actually print anything on the output object.
e10a6a0a59f035e870d90cca05b85fce.gif
' Print a string on a Printer or PictureBox,
' wrapped within the margins.
Private Sub PrintWrappedText(ByVal ptr As Object, _
    ByVal target_page As Integer, ByVal txt As String, _
    ByVal indent As Single, ByVal left_margin As Single, _
    ByVal top_margin As Single, _
    ByVal right_margin As Single, _
    ByVal bottom_margin As Single)

Dim next_paragraph As String
Dim next_word As String
Dim pos As Integer
Dim current_page As Integer

    ' Start at the top of the page.
    Printer.CurrentY = top_margin

    ' Keep track of the page we are on.
    current_page = 1

    ' Repeat until the text is all printed.
    Do While Len(txt) > 0
        ' Get the next paragraph. 
        pos = InStr(txt, vbCrLf) 
        If pos = 0 Then 
            ' Use the rest of the text. 
            next_paragraph = Trim$(txt) 
            txt = ""
        Else
            ' Get the paragraph. 
            next_paragraph = Trim$(Left$(txt, pos - 1)) 
            txt = Mid$(txt, pos + Len(vbCrLf)) 
        End If 

        ' Indent the paragraph. 
        Printer.CurrentX = left_margin + indent 

        ' Print the paragraph.
        Do While Len(next_paragraph) > 0

 
< previous page page_153 next page >